package com.qnw.CardGroupManagement;

import android.app.*;
import android.os.*;
import android.text.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import com.qnw.CardGroupManagement.*;
import android.view.ViewGroup.*;
import com.ygo.feihua.R;
import com.ygo.feihua.Gongju;
import android.support.v7.widget.Toolbar;
import android.support.v7.app.AppCompatActivity;

public class D5 extends AppCompatActivity implements OnClickListener
{
	private Dialog builder;

	private TextView tv_w1;
	private TextView tv_w2;
	private TextView tv_w3;
	private TextView tv_w4;
	//显示
	private TextView tv_display;
	//数值
	private EditText et_number;
	//单选框
	private RadioGroup rg_radio;
	private RadioButton rb_w1;
	private RadioButton rb_w2;
	private RadioButton rb_w3;
	private RadioButton rb_w4;
	//加
	private Button bu_plus;
	private Button bu_plus50;
	private Button bu_plus100;
	private Button bu_plus500;
	private Button bu_plus1000;
	//减
	private Button bu_reduce;
	private Button bu_reduce50;
	private Button bu_reduce100;
	private Button bu_reduce500;
	private Button bu_reduce1000;
	//加入决斗
	private Button bu_join;
	//退出决斗
	private Button bu_Signout;
	//减半
	private Button bu_halve;
	//加倍
	private Button bu_double;
	//骰子
	private Button bu_dice;
	//硬币
	private Button bu_coin;
	private Gongju gj;

	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		this.setContentView(R.layout.d5_layout);
		tv_w1 = (TextView)findViewById(R.id.d5_tv_w1);
		tv_w2 = (TextView)findViewById(R.id.d5_tv_w2);
		tv_w3 = (TextView)findViewById(R.id.d5_tv_w3);
		tv_w4 = (TextView)findViewById(R.id.d5_tv_w4);
		tv_display = (TextView)findViewById(R.id.d5_tv_display);
		et_number = (EditText)findViewById(R.id.d5_et_number);
		rg_radio = (RadioGroup)findViewById(R.id.d5_rg_radio);
		rb_w1 = (RadioButton)findViewById(R.id.d5_rb_w1);
		rb_w2 = (RadioButton)findViewById(R.id.d5_rb_w2);
		rb_w3 = (RadioButton)findViewById(R.id.d5_rb_w3);
		rb_w4 = (RadioButton)findViewById(R.id.d5_rb_w4);
		bu_plus = (Button)findViewById(R.id.d5_bu_plus);
		bu_plus50 = (Button)findViewById(R.id.d5_bu_plus50);
		bu_plus100 = (Button)findViewById(R.id.d5_bu_plus100);
		bu_plus500 = (Button)findViewById(R.id.d5_bu_plus500);
		bu_plus1000 = (Button)findViewById(R.id.d5_bu_plus1000);
		bu_reduce = (Button)findViewById(R.id.d5_bu_reduce);
		bu_reduce50 = (Button)findViewById(R.id.d5_bu_reduce50);
		bu_reduce100 = (Button)findViewById(R.id.d5_bu_reduce100);
		bu_reduce500 = (Button)findViewById(R.id.d5_bu_reduce500);
		bu_reduce1000 = (Button)findViewById(R.id.d5_bu_reduce1000);
		bu_join = (Button)findViewById(R.id.d5_bu_join);
		bu_Signout = (Button)findViewById(R.id.d5_bu_Signout);
		bu_halve = (Button)findViewById(R.id.d5_bu_halve);
		bu_double = (Button)findViewById(R.id.d5_bu_double);
		bu_dice = (Button)findViewById(R.id.d5_bu_dice);
		bu_coin = (Button)findViewById(R.id.d5_bu_coin);
		Toolbar tool=(Toolbar) findViewById(R.id.toolbar);
		
		gj=Gongju.getdx(this);
		
		gj.cshToolbar(tool,"决斗工具");
		
		tv_display.setOnClickListener(this);
		bu_plus.setOnClickListener(this);
		bu_plus50.setOnClickListener(this);
		bu_plus100.setOnClickListener(this);
		bu_plus500.setOnClickListener(this);
		bu_plus1000.setOnClickListener(this);
		bu_reduce.setOnClickListener(this);
		bu_reduce50.setOnClickListener(this);
		bu_reduce100.setOnClickListener(this);
		bu_reduce500.setOnClickListener(this);
		bu_reduce1000.setOnClickListener(this);
		bu_join.setOnClickListener(this);
		bu_Signout.setOnClickListener(this);
		bu_halve.setOnClickListener(this);
		bu_double.setOnClickListener(this);
		bu_dice.setOnClickListener(this);
		bu_coin.setOnClickListener(this);
	}


	public void onClick(View p1)
	{
		switch (p1.getId())
		{
			case R.id.d5_bu_join:
				{
					TextView t=getTextView();
					String[] ss=t.getText().toString().split(":");
					if (ss[1].equals("0"))
					{
						t.setText(ss[0] + ":8000");
					}
					break;
				}
			case R.id.d5_bu_Signout:
				{
					TextView t=getTextView();
					String[] ss=t.getText().toString().split(":");
					t.setText(ss[0] + ":0");
					break;
				}
			case R.id.d5_bu_halve:
				{
					TextView t=getTextView();
					String[] ss=t.getText().toString().split(":");
					int i=Integer.valueOf(ss[1]);
					i = i / 2;
					t.setText(ss[0] + ":" + i);
					break;
				}
			case R.id.d5_bu_double:
				{
					TextView t=getTextView();
					String[] ss=t.getText().toString().split(":");
					int i=Integer.valueOf(ss[1]);
					i = i * 2;
					t.setText(ss[0] + ":" + i);
					break;
				}
			case R.id.d5_bu_plus:
				{
					TextView t=getTextView();
					String[] ss=t.getText().toString().split(":");
					if (ss[1].equals("0"))
					{
						break;
					}
					if (TextUtils.isEmpty(et_number.getText().toString()))
					{
						break;
					}
					int i=Integer.valueOf(ss[1]);
					int j=Integer.valueOf(et_number.getText().toString());
					i = i + j;
					t.setText(ss[0] + ":" + i);
					break;
				}
			case R.id.d5_bu_plus50:
				{
					TextView t=getTextView();
					String[] ss=t.getText().toString().split(":");
					if (ss[1].equals("0"))
					{
						break;
					}
					int i=Integer.valueOf(ss[1]);
					i = i + 50;
					t.setText(ss[0] + ":" + i);
					break;
				}
			case R.id.d5_bu_plus100:
				{
					TextView t=getTextView();
					String[] ss=t.getText().toString().split(":");
					if (ss[1].equals("0"))
					{
						break;
					}
					int i=Integer.valueOf(ss[1]);
					i = i + 100;
					t.setText(ss[0] + ":" + i);
					break;
				}
			case R.id.d5_bu_plus500:
				{
					TextView t=getTextView();
					String[] ss=t.getText().toString().split(":");
					if (ss[1].equals("0"))
					{
						break;
					}
					int i=Integer.valueOf(ss[1]);
					i = i + 500;
					t.setText(ss[0] + ":" + i);
					break;
				}
			case R.id.d5_bu_plus1000:
				{
					TextView t=getTextView();
					String[] ss=t.getText().toString().split(":");
					if (ss[1].equals("0"))
					{
						break;
					}
					int i=Integer.valueOf(ss[1]);
					i = i + 1000;
					t.setText(ss[0] + ":" + i);
					break;
				}
			case R.id.d5_bu_reduce:
				{
					TextView t=getTextView();
					String[] ss=t.getText().toString().split(":");
					if (ss[1].equals("0"))
					{
						break;
					}
					if (TextUtils.isEmpty(et_number.getText().toString()))
					{
						break;
					}
					int i=Integer.valueOf(ss[1]);
					int j=Integer.valueOf(et_number.getText().toString());
					i = i - j;
					t.setText(ss[0] + ":" + i);
					break;
				}
			case R.id.d5_bu_reduce50:
				{
					TextView t=getTextView();
					String[] ss=t.getText().toString().split(":");
					if (ss[1].equals("0"))
					{
						break;
					}
					int i=Integer.valueOf(ss[1]);
					i = i - 50;
					t.setText(ss[0] + ":" + i);
					break;
				}
			case R.id.d5_bu_reduce100:
				{
					TextView t=getTextView();
					String[] ss=t.getText().toString().split(":");
					if (ss[1].equals("0"))
					{
						break;
					}
					int i=Integer.valueOf(ss[1]);
					i = i - 100;
					t.setText(ss[0] + ":" + i);
					break;
				}
			case R.id.d5_bu_reduce500:
				{
					TextView t=getTextView();
					String[] ss=t.getText().toString().split(":");
					if (ss[1].equals("0"))
					{
						break;
					}
					int i=Integer.valueOf(ss[1]);
					i = i - 500;
					t.setText(ss[0] + ":" + i);
					break;
				}
			case R.id.d5_bu_reduce1000:
				{
					TextView t=getTextView();
					String[] ss=t.getText().toString().split(":");
					if (ss[1].equals("0"))
					{
						break;
					}
					int i=Integer.valueOf(ss[1]);
					i = i - 1000;
					t.setText(ss[0] + ":" + i);
					break;
				}
			case R.id.d5_bu_dice:
				{	
					builder = new Dialog(this);
					builder.setTitle("掷骰子");
					builder.show();

					LayoutInflater inflater = LayoutInflater.from(this);
					View viewDialog = inflater.inflate(R.layout.dialog_ys, null);
					Display display = this.getWindowManager().getDefaultDisplay();
					int width = display.getWidth();
					int hight=display.getHeight();
					//设置对话框的宽高
					LayoutParams layoutParams = new LayoutParams(width * 73 / 100, LayoutParams.WRAP_CONTENT);
					builder.setContentView(viewDialog, layoutParams);
					GifView gv=(GifView)viewDialog.findViewById(R.id.d5_gif_ys);
					int i=(int)(Math.random() * 6)+1;
					switch(i)
					{
						case 1:
						{
							gv.setMovieResource(R.drawable.image_8);
							break;
						}
						case 2:
							{
								gv.setMovieResource(R.drawable.image_9);
								break;
							}
						case 3:
							{
								gv.setMovieResource(R.drawable.image_10);
								break;
							}
						case 4:
							{
								gv.setMovieResource(R.drawable.image_11);
								break;
							}
						case 5:
							{
								gv.setMovieResource(R.drawable.image_12);
								break;
							}
						case 6:
							{
								gv.setMovieResource(R.drawable.image_13);
								break;
							}
					}
					break;
				}
			case R.id.d5_bu_coin:
				{
					builder = new Dialog(this);
					builder.setTitle("抛硬币");
					builder.show();

					LayoutInflater inflater = LayoutInflater.from(this);
					View viewDialog = inflater.inflate(R.layout.dialog_ys, null);
					Display display = this.getWindowManager().getDefaultDisplay();
					int width = display.getWidth();
					int hight=display.getHeight();
					//设置对话框的宽高
					LayoutParams layoutParams = new LayoutParams(width * 73 / 100, LayoutParams.WRAP_CONTENT);
					builder.setContentView(viewDialog, layoutParams);
					ImageView im=(ImageView)viewDialog.findViewById(R.id.d5_im_ys);
					TextView tv=(TextView)viewDialog.findViewById(R.id.d5_tv_ys);
					int i=(int)(Math.random() * 2);
					if (i == 0)
					{
						im.setImageDrawable(getResources().getDrawable(R.drawable.image_6));
						tv.setText("正面");
					}
					else
					{
						tv.setText("反面");
						im.setImageDrawable(getResources().getDrawable(R.drawable.image_7));
					}
					break;
				}
			case R.id.d5_tv_display:
				{
					break;
				}
		}
	}	

	private TextView getTextView()
	{
		TextView t=tv_w1;
		for (int i=0; i < rg_radio.getChildCount(); i++)
		{  
			RadioButton r = (RadioButton)rg_radio.getChildAt(i);  
			if (r.isChecked())
			{  
				String s = r.getText().toString();  
				if (s.equals("玩家2"))
				{
					t = tv_w2;
				}
				else if (s.equals("玩家3"))
				{
					t = tv_w3;
				}
				else if (s.equals("玩家4"))
				{
					t = tv_w4;
				}
				break;
			}  
		} 
		return t;
	}
}
